package com.platform.module.service.impl;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.constant.UserConstants;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.DateUtils;
import com.platform.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.platform.module.mapper.BaseFactoryMapper;
import com.platform.module.domain.BaseFactory;
import com.platform.module.service.IBaseFactoryService;
import com.platform.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 厂商信息管理Service业务层处理
 * 
 * @author 王琦
 * @date 2020-06-20
 */
@Service
public class BaseFactoryServiceImpl extends ServiceImpl<BaseFactoryMapper,BaseFactory> implements IBaseFactoryService
{
    @Autowired
    private BaseFactoryMapper baseFactoryMapper;

    /**
     * 查询厂商信息管理列表
     * 
     * @param baseFactory 厂商信息管理
     * @return 厂商信息管理
     */
    @Override
    public List<BaseFactory> selectBaseFactoryList(BaseFactory baseFactory)
    {
        return baseFactoryMapper.selectBaseFactoryList(baseFactory);
    }

    /**
     * 新增厂商信息管理
     * 
     * @param baseFactory 厂商信息管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBaseFactory(BaseFactory baseFactory)
    {
        baseFactory.setCreateTime(DateUtils.getNowDate());
        return baseFactoryMapper.insert(baseFactory);
    }

    /**
     * 修改厂商信息管理
     * 
     * @param baseFactory 厂商信息管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBaseFactory(BaseFactory baseFactory)
    {
        baseFactory.setUpdateTime(DateUtils.getNowDate());
        return baseFactoryMapper.updateById(baseFactory);
    }

    /**
     * 删除厂商信息管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBaseFactoryByIds(String ids)
    {
        return baseFactoryMapper.deleteBatchIds(Arrays.asList(Convert.toStrArray(ids)));
    }

    /**
     * 校验厂商代码是否唯一
     *
     * @param baseFactory 厂商信息
     * @return 结果
     */
    @Override
    public String checkFactoryCodeUnique(BaseFactory baseFactory)
    {
        Long factoryId = StringUtils.isNull(baseFactory.getFactoryId()) ? -1L : baseFactory.getFactoryId();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("factory_code",baseFactory.getFactoryCode());
        BaseFactory factory = baseFactoryMapper.selectOne(queryWrapper);
        if (StringUtils.isNotNull(factory) && factory.getFactoryId().longValue() != factoryId.longValue())
        {
            return UserConstants.FACTORY_CODE_NOT_UNIQUE;
        }
        return UserConstants.FACTORY_CODE_UNIQUE;
    }

    /**
     * 导入厂商信息数据
     *
     * @param factoryList 厂商信息数据
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    @Transactional
    public String importFactory(List<BaseFactory> factoryList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(factoryList) || factoryList.size() == 0)
        {
            throw new BusinessException("导入厂商信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BaseFactory factory : factoryList)
        {
            try
            {
                // 验证是否存在这个厂商
                BaseFactory f = baseFactoryMapper.selectOne(new QueryWrapper<BaseFactory>().eq("factory_code",factory.getFactoryCode()));
                if (StringUtils.isNull(f))
                {
                    factory.setCreateBy(operName);
                    factory.setCreateTime(DateUtils.getNowDate());
                    this.insertBaseFactory(factory);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、厂商 " + factory.getFactoryName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    factory.setUpdateBy(operName);
                    factory.setUpdateTime(DateUtils.getNowDate());
                    this.updateBaseFactory(factory);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、厂商 " + factory.getFactoryName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、厂商 " + factory.getFactoryName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、厂商 " + factory.getFactoryName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
